//
//  WXLPlayMVViewController.m
//  段子手
//
//  Created by dllo on 16/3/23.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "WXLPlayMVViewController.h"

@interface WXLPlayMVViewController ()<UITableViewDataSource, UITableViewDelegate, PlayMusicVideoViewDelegate>

@property (nonatomic, strong) UIVisualEffectView *visualEfView; /**< 毛玻璃 */
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) PlayMusicVideoView *playMVView; /**< 视频播放 */

@property (nonatomic, retain) UILabel *label; // 标题


@property (nonatomic, retain) NSMutableArray *dataArr; /**< 总数据 */

@end

@implementation WXLPlayMVViewController

+ (WXLPlayMVViewController *)wxlPlayMVViewController {
    static WXLPlayMVViewController *wxlBVC = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        wxlBVC = [[WXLPlayMVViewController alloc] init];
    });
    return wxlBVC;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
}

- (void)setSongsModel:(WXLSongsModel *)songsModel {
    if (_songsModel != songsModel) {
        _songsModel = songsModel;
    }
    [self dataHandelFromUrl:_songsModel.videoId];
    
    _playMVView.songsModel = _songsModel;
    
    _label.text = [NSString stringWithFormat:@"%@ - %@", _songsModel.name, _songsModel.singerName];
}


#pragma mark - 子视图
- (void)createSubView {
    // 毛玻璃
    [self createVisualEfView];
    
    // 创建 播放
    [self createPlayView];
    
    // 创建tableView
    [self createTableView];
    
}

- (void)createPlayView {
    self.playMVView = [PlayMusicVideoView playMusicVideoViewWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight / 3)];
    
    // 全屏播放结束回调该block
    __strong typeof(self) weakSelf = self;
    self.playMVView.block = ^{
        weakSelf.tableView.hidden = NO;
    };
    
    _playMVView.delegate = self;
    [self.view addSubview:_playMVView];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _playMVView.frame.size.height, kUIScreenWidth, kUIScreenHeight - _playMVView.frame.size.height) style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    _tableView.bounces = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[WXLPlayVideoTableViewCell class] forCellReuseIdentifier:@"WXLPlayVideoTableViewCell"];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 100)];
    view.backgroundColor = [UIColor clearColor];
    _tableView.tableHeaderView = view;
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, kUIScreenWidth - 40, 50)];
    _label.backgroundColor = [UIColor clearColor];
    _label.text = [NSString stringWithFormat:@"%@ - %@", _songsModel.name, _songsModel.singerName];
    [view addSubview:_label];
    _label.textColor = [UIColor blackColor];
    
    UILabel *label_title = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, kUIScreenWidth - 40, 50)];
    label_title.backgroundColor = [UIColor clearColor];
    label_title.text = @"推荐MV";
    label_title.font = [UIFont systemFontOfSize:25];
    [view addSubview:label_title];
}


// 毛玻璃
- (void)createVisualEfView {
    // 毛玻璃
    self.visualEfView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    _visualEfView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _visualEfView.alpha = 1;
    _visualEfView.userInteractionEnabled = YES;
    
    [self.view addSubview:_visualEfView];
}



#pragma mark - tabeleView Delegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WXLPlayVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WXLPlayVideoTableViewCell"];
    cell.backgroundColor = [UIColor clearColor];
    cell.model = _dataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 100 大图高 20上下各距10
    return 100 + 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WXLSongsModel *model = _dataArr[indexPath.row];
    
    _playMVView.songsModel = model;
    
    _label.text = [NSString stringWithFormat:@"%@ - %@", model.videoName, model.singerName];
    
    [self dataHandelFromUrl:model.videoId];
}



#pragma mark - 点击
// 实现返回上一页
- (void)buttonToAction:(UIButton *)button {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark PlayMusicVideoViewDelegate
- (void)fullScreenButtonActionFull:(UIButton *)button fullButton:(UIButton *)fullButton{
    if ([UIDevice currentDevice].orientation != UIDeviceOrientationPortrait) {
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        _tableView.hidden = NO;
        button.hidden = NO;
        _playMVView.frame = CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight / 3);
        [fullButton setBackgroundImage:[UIImage imageNamed:@"iconfont-quanping"] forState:(UIControlStateNormal)];
    } else {
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        _tableView.hidden = YES;
        button.hidden = YES;
        _playMVView.frame = CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight);
        [fullButton setBackgroundImage:[UIImage imageNamed:@"iconfont-shouqiquanping"] forState:(UIControlStateNormal)];
    }
}

- (void)buttonToActionClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark - 数据处理
- (void)dataHandelFromUrl:(NSString *)mVID {
    NSString *urlStr = [NSString stringWithFormat:@"http://api.dongting.com/sim/mv/%@/similarity?utdid=VGGJcU4veJsDACPAEYL62k1V&v=v8.4.0.2016022715&f=f168&mid=MI%%2B3W&tid=0", mVID];
    [WxlAPPTools GETWithURL:urlStr par:nil success:^(id responseObject) {
        
        NSArray *tempArr = [WXLSongsModel modelHandelWithArray:responseObject[@"data"]];
        if (!tempArr.count) {
            return;
        }
        
        if (_dataArr.count) {
            [_dataArr removeAllObjects];
        }
        
        for (WXLSongsModel *model in tempArr) {
            [_dataArr addObject:model];
        }
        [_tableView reloadData];
        
    } filed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
        self.dataArr = [NSMutableArray new];
        
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"beijing.jpg"]];
        
        // 创建子视图
        [self createSubView];
    }
    return self;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
